package cate.game.pvp.minecraft;

import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cate.game.play.proce.play.PlayPO;
import cate.game.pvp.minecraft.MinecraftMessagingModule.CrossAddMail;
import cate.game.role.Role;
import cp.solution.ops.status.ModOprStatus;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Getter
@NoteClass("矿洞玩法全服管理")
public class MinecraftManager extends ModuleMan {
    public static final String MODULE_NAME = "minecraft";
    /**
     * 是否需要合服处理
     */
    public static boolean MERGE_EXE = false;

    private final MinecraftDataManager dataManager = new MinecraftDataManager();
    private final MinecraftMessagingModule messagingModule = new MinecraftMessagingModule();
    private final MinecraftReceiveModule notificationModule = new MinecraftReceiveModule();

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        this.dataManager.initIndependent(game);
        this.notificationModule.initIndependent(game);
        this.messagingModule.initIndependent(game);
    }

    @Override
    public void initDependent() throws Exception {
        this.dataManager.initDependent();
        this.notificationModule.initDependent();
        this.messagingModule.initDependent();
        MinecraftConstants.loadFromTable(game.table.common);
    }

    @Override
    public void shutdown() {
        super.shutdown();
        this.messagingModule.shutdown();
        this.dataManager.shutdown();
    }

    /**
     * 更新玩法关闭状态
     *
     * @param close 是否关闭
     */
    public void updateMinecraftClosed(boolean close, long openTime, long closedTime) {
        this.dataManager.getKv().close = close;
        this.dataManager.getKv().setOpenTime(openTime);
        this.dataManager.getKv().setCloseTime(closedTime);
        // 全服通知
        game.role.activeForEach(role -> role.getPvp().minecraft.sendMinecraftMineAreaState());
    }

    /**
     * @return 是否关闭
     */
    public boolean isClosed() {
        return this.dataManager.getKv().close;
    }

    /**
     * @return 获取当前轮开始时间
     */
    public long getOpenTime() {
        return this.dataManager.getKv().getOpenTime();
    }

    /**
     * @return 获取当前轮结束时间
     */
    public long getCloseTime() {
        return this.dataManager.getKv().getCloseTime();
    }

    @Override
    public ModOprStatus handleMergeSettle() {
        ModOprStatus status = new ModOprStatus("Minecraft");
        return status.exe(() -> {
            if (MERGE_EXE) {
                List<CrossAddMail> mails = getMessagingModule().mergeSettle();
                if (mails == null || mails.isEmpty()) {
                    log.info("[MinecraftManager] 跨服结算没有邮件要发送");
                    return;
                }
                log.info("[MinecraftManager] 跨服结算共 {} 封邮件发送", mails.size());
                mails.forEach(cam -> {
                    game.mail.send(cam.roleUid, cam.mail);
                });
            } else {
                log.info("[MinecraftManager] 暂时不进行合服处理");
            }
        });
    }
}
